<?php

namespace App\Http\Resources\goods;


use App\Models\goods\ShopCar;
use App\Models\User;
use Illuminate\Http\Resources\Json\ResourceCollection;

class ShopCarCollection extends ResourceCollection
{

    protected $user;

    public function __construct($resource,User $user)
    {
        parent::__construct($resource);

        $this->resource = $this->collectResource($resource);
        $this->user = $user;
    }


    /**
     * Transform the resource collection into an array.
     *
     * @return \Illuminate\Support\Collection
     */
    public function toArray($request)
    {
        return $this->collection->map(function (ShopCar $item) {
            return [
                'id' => $item->id,
                'goods_id' => $item->goods_id,
                'type_name' => $item->type_name??'',
                'num' => $item->num,
                'goods_spu_id' => $item->goods->spu_id,
                'goods_img' => $item->goods->img,
                'goods_name' => $item->goods->name,
                'goods_price' => getPrice($item,$this->user),
                'old_price' => $item->goods->old_price,
                'stock' => $item->goods->stock,
                'goods_spec' => $item->spec->name??'',
                'updated_at' => $item->updated_at->todateTimeString()??'',
                'checked' => $item->checked == 1 ? true : false,
            ];
        });
    }
}
